import entity.Employee;
import org.junit.Test;

import java.util.Optional;

/**
 * 一、Optional 容器类：用于尽量避免空指针异常
 * Optional.of(T t) : 创建一个 Optional 实例
 * Optional.empty() : 创建一个空的 Optional 实例
 * Optional.ofNullable(T t):若 t 不为 null,创建 Optional 实例,否则创建空实例
 * isPresent() : 判断是否包含值
 * orElse(T t) :  如果调用对象包含值，返回该值，否则返回t
 * orElseGet(Supplier s) :如果调用对象包含值，返回该值，否则返回 s 获取的值
 * map(Function f): 如果有值对其处理，并返回处理后的Optional，否则返回 Optional.empty()
 * flatMap(Function mapper):与 map 类似，要求返回值必须是Optional
 */
public class TestOptional {
    @Test
    public void test01() {
        Optional<Employee> optionalEmployee = Optional.of(new Employee());
        Employee employee = optionalEmployee.get();
        System.out.println(employee);
    }

    @Test
    public void test02() {
        Optional<Employee> optionalEmployee = Optional.empty();
        System.out.println(optionalEmployee.get());
    }

    @Test
    public void test03() {
        Optional<Employee> optionalEmployee = Optional.ofNullable(null);
        if (optionalEmployee.isPresent()) {
            System.out.println(optionalEmployee.get());
        }
        Employee employee = optionalEmployee.orElse(new Employee("Lilei"));
        System.out.println(employee);

        Employee employee1 = optionalEmployee.orElseGet(() -> new Employee());
        System.out.println(employee1);
    }

    @Test
    public void test04() {
        Optional<Employee> op = Optional.of(new Employee(101, "张三", 18, 9999.99));
        Optional<String> op1 = op.map(Employee::getName);
        System.out.println(op1.get());

        Optional<String> op2 = op.flatMap((e) -> Optional.of(e.getName()));
        System.out.println(op2.get());
    }

    @Test
    public void test05() {
        Man man = new Man();
        String name = getGodnessName(man);
        System.out.println(name);
    }

    public String getGodnessName(Man man) {
        if (man != null) {
            Godness godness = man.getGod();
            if (godness != null) {
                return godness.getName();
            }
        }
        return "c teacher";
    }

    @Test
    public void test06(){
        Optional<Godness> godness = Optional.ofNullable(new Godness("LZL"));
        Optional<NewMan> newMan = Optional.ofNullable(new NewMan(godness));
        String name = getGodnessName2(newMan);
        System.out.println(name);
    }

    public String getGodnessName2(Optional<NewMan> newMan) {
        return newMan.orElse(new NewMan())
                .getGodness()
                .orElse(new Godness("c teacher"))
                .getName();
    }
}

class Man {

    private Godness god;

    public Man() {
    }

    public Man(Godness god) {
        this.god = god;
    }

    public Godness getGod() {
        return god;
    }

    public void setGod(Godness god) {
        this.god = god;
    }

    @Override
    public String toString() {
        return "Man [god=" + god + "]";
    }

}

// 注意：Optional 不能被序列化
class NewMan {
    private Optional<Godness> godness = Optional.empty();

    private Godness god;

    public Optional<Godness> getGod() {
        return Optional.of(god);
    }

    public NewMan() {
    }

    public NewMan(Optional<Godness> godness) {
        this.godness = godness;
    }

    public Optional<Godness> getGodness() {
        return godness;
    }

    public void setGodness(Optional<Godness> godness) {
        this.godness = godness;
    }

    @Override
    public String toString() {
        return "NewMan [godness=" + godness + "]";
    }

}

class Godness {

    private String name;

    public Godness() {
    }

    public Godness(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Godness [name=" + name + "]";
    }

}
